/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.pc9.pswing.util;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;

/**
 *
 * @author paulocanedo
 */
public class ImageresReader {

    private static ImageresReader instance;
    private static final int[] pngIntStart = new int[]{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
    private static byte[] pngBytesStartList = new byte[8];
    private static final int[] pngIntEnd = new int[]{0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82};
    private static byte[] pngBytesEndList = new byte[8];
    private static File imageres = new File(WindowsUtil.getWindowsPath() + "/system32/imageres.dll");
    private List<Image> images = new ArrayList<Image>();

    static {
        for (int i = 0; i < pngIntStart.length; i++) {
            pngBytesStartList[i] = ((byte) pngIntStart[i]);
        }

        for (int i = 0; i < pngIntEnd.length; i++) {
            pngBytesEndList[i] = ((byte) pngIntEnd[i]);
        }
    }

    private ImageresReader() throws FileNotFoundException, IOException {
    }

    public static ImageresReader getInstance() throws FileNotFoundException, IOException {
        if (instance == null) {
            instance = new ImageresReader();
            instance.parseAllImages();
        }
        return instance;
    }

    public Image getImage(int index) {
        return images.get(index);
    }

    private void parseAllImages() throws FileNotFoundException, IOException {
        int byteControl = -1;
        byte[] bytes = new byte[(int) imageres.length()];
        FileInputStream input = new FileInputStream(imageres);

        int readed;
        byte[] buffer = new byte[1024 * 32];
        while ((readed = input.read(buffer)) > 0) {
            for (int i = 0; i < readed; i++) {
                bytes[++byteControl] = buffer[i];
            }
        }
        Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
        int fromIndex = 0;
        while (true) {
            int start = indexOfSubList(bytes, pngBytesStartList, fromIndex);
            int end = indexOfSubList(bytes, pngBytesEndList, fromIndex);
            if (start < 0 || end < 0) {
                break;
            }

            end += pngBytesEndList.length;
            Image image = defaultToolkit.createImage(bytes, start, end);
            images.add(image);

            fromIndex = end;
        }
    }

    private static int indexOfSubList(byte[] source, byte[] target, int fromIndex) {
        int sourceSize = source.length;
        int targetSize = target.length;
        int maxCandidate = sourceSize - targetSize;

        nextCand:
        for (int candidate = fromIndex; candidate <= maxCandidate; candidate++) {
            for (int i = 0, j = candidate; i < targetSize; i++, j++) {
                if (!eq(target[i], source[j])) {
                    continue nextCand;  // Element mismatch, try next cand
                }
            }
            return candidate;  // All elements of candidate matched target
        }
        return -1;  // No candidate matched the target
    }

    private static boolean eq(Object o1, Object o2) {
        return (o1 == null ? o2 == null : o1.equals(o2));
    }
    public final static int FILE_SYSTEM = 0;
    public final static int DEFAULT_FOLDER = 1;
    public final static int MUSIC_FOLDER = 110;
    public final static int DOCUMENTS_FOLDER = 114;
    public final static int PICTURES_FOLDER = 115;
    public final static int MOVIES_FOLDER = 116;
    public final static int FAVORITE_FOLDER = 118;
    public final static int USER_FOLDER = 122;
    public final static int DOWNLOAD_FOLDER = 180;

    public static void main(String... args) {
        try {
            //            long currentTimeMillis = System.currentTimeMillis();
            //            ImageresReader imageresReader = ImageresReader.getInstance();
            //            Image imageIcon = imageresReader.getImage(FILE_SYSTEM);
            //            System.out.println(System.currentTimeMillis() - currentTimeMillis);
            WindowsUtil windowsUtil = new WindowsUtil();
            Icon systemIcon = windowsUtil.getSystemIcon("places", "folder", 16);

            javax.swing.JOptionPane.showMessageDialog(null, systemIcon);
        } catch (Exception ex) {
            Logger.getLogger(ImageresReader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
